package com.hundsun.s3.client.pub.common;

import com.hundsun.jresplus.middleware.BaseBizRuntimeException;
import com.hundsun.jresplus.middleware.MiddlewareService;
import com.hundsun.jresplus.middleware.ResultSet;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by xiaowx23615 on 2018/1/31.
 */
public class BaseDao {
    @Autowired
    private MiddlewareService middlewareService;

    public MiddlewareService getMiddlewareService(){
        return middlewareService;
    }

    /**
     * 调用中间件的功能 ,此方法只是对中间件call方法的简单封转，未做任何处理，除非exc，get，qry不能满足要求尽量不要调用此方法
     * @param functionId
     * @param parm
     * @return
     */
    protected List<Map<String, String>> call(String functionId, Map<String, Object> parm){
        ResultSet rs = middlewareService.call(functionId, parm);
        return rs.getDataset();
    }

    /**
     * 调用中间件的执行功能  注：查询功能不能调用此方法
     * @param functionId
     * @param parm
     * @return
     */
    protected  String exc(String functionId,Map<String, Object> parm) {
        String resultMsg = null;
        try {
        ResultSet resultSet = middlewareService.call(functionId, parm);
        List<Map<String,String>> list = resultSet.getDataset();
        if(list == null){
            throw new BaseBizRuntimeException("-1","执行中间件返回值为空！","");
        }
        if(list.size() == 1){
            //中间件执行成功，有返回值
            Map<String,String> returnMap = list.get(0);
            Set set = returnMap.keySet();
            Iterator i = set.iterator();
            if(i.hasNext()) {
                resultMsg = returnMap.get(i.next());
            }
//            List<String> result = (List<String>) returnMap.values();
//            resultMsg = result.get(0);
        }else if(list.isEmpty()){
            //中间件执行成功，没有返回值
            resultMsg = "执行成功";
        }else{

            throw new BaseBizRuntimeException("-1","执行中间件返回值记录过多！","");
        }
    }catch(BaseBizRuntimeException e){
            resultMsg = e.getErrorNo()+"错误信息："+e.getErrorInfo()+"错误路径："+e.getErrorPathInfo();
//        item = new MsgItem(e.getErrorNo(),"错误信息："+e.getErrorInfo()+" <br>错误路径："+e.getErrorPathInfo());
    }

    return resultMsg;

    }
    /**
     * 获取单个实体
     * @param <T>
     * @param functionId
     * @param parm
     * @return
     * @return
     */
    protected  <T> T get(String functionId,Map<String, Object> parm,Class<T> clazz){
        ResultSet rs = middlewareService.call(functionId, parm);
        List<T> list = rs.getDataset(clazz);
        if(list == null){
            throw new BaseBizRuntimeException("-1","执行中间件返回值为空！","");
        }
        if(list.size() ==1){
            return list.get(0);
        }else if(list.isEmpty()){
            //此处不做异常处理，因为没有数据并不代表错误
            return null;
        }else{
            throw new BaseBizRuntimeException("-1","执行中间件返回值记录过多！","");
        }
    }
    /**
     * 调用中间件的查询功能,返回结果集为对应对象  注：执行功能不能调用此方法
     * @param <T>
     * @param functionId
     * @param parm
     * @return
     */
    protected <T> List<T> qry(String functionId,Map<String, Object> parm,Class<T> clazz){
        ResultSet rs = middlewareService.call(functionId, parm);
        return rs.getDataset(clazz);
    }
}
